{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d3cf23c8",
   "metadata": {},
   "source": [
    "## numpy函数的讲解-聚合函数\n",
    "\n",
    "numpy函数分为一元函数、二元函数和聚合函数。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "beda00fb",
   "metadata": {},
   "source": [
    "3. 聚合函数：求数组的总和、平均值、排序等\n",
    "\n",
    " * 理解聚合函数需要理解numpy的axis轴特性。\n",
    " * numpy.sum()和ndarray.sum()函数可以通用，除了在使用numpy.sum()计算pyhton序列的数组时。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c7a048c",
   "metadata": {},
   "source": [
    "|序号||  函数   | | 安全函数| | 描述  |\n",
    "|----||  :----  | | :----  |  | ----:  |\n",
    "|1|| np.sum | | np.nansum | | 计算元素的和。np.nansum是一种安全算法，排除nan值之后的相加 |\n",
    "|2||  np.prod | | np.nanprod  |  | 计算元素的乘积  |\n",
    "|3||  np.mean | | np.nanmean  |  |  计算元素的平均值 |\n",
    "|4||  np.stu | | np.nanstu  |  | 计算元素的标准差  |\n",
    "|5|| np.var  | |  np.nanstu |  |  计算元素的方差 |\n",
    "|6||  np.min | |  np.nanmin |  |  计算元素的最小值 |\n",
    "|7||  np.max | | np.nanmax  |  |  计算元素的最大值 |\n",
    "|8|| np.argmin  | |  np.nanargmin |  |  找出最小值元素的索引|\n",
    "|9||  np.argmax | |  np.nanargmax |  |  找出最大值元素的索引 |\n",
    "|10|| np.median  | |  np.nanmedian |  | 计算元素的中位数  |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2a8f310e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "692fc0e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 7, 3],\n",
       "       [0, 0, 6]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d1 = np.random.randint(0,9,size=(2,3))\n",
    "d1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "423e564f",
   "metadata": {},
   "source": [
    "1. np.sum()函数。等同于ndarry.sum()。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9bf1ab25",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "24"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 当不输入axis参数时，全部数据求和\n",
    "d1.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f0de938e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 7, 9])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用np.sum需要指定axis参数的值，即求和的轴\n",
    "np.sum(d1,axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9883e941",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([18,  6])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用np.sum需要指定axis参数的值，即求和的轴\n",
    "np.sum(d1,axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f64d2f8",
   "metadata": {},
   "source": [
    "2. np.prod()函数。等同于ndarry.prod()函数，求数据乘积。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "74a1516f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 1, 2],\n",
       "       [6, 1, 4]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d2=np.random.randint(1,9,size=(2,3))\n",
    "d2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8d5c96c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "288"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#求积np.prod()，不输入轴时是全部数据的乘积。\n",
    "np.prod(d2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "3e669836",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 2, 8])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d2.prod(axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad607ba9",
   "metadata": {},
   "source": [
    "3. 求平均值函数np.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "6e3e9d54",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 4, 6],\n",
       "       [7, 0, 3]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d3=np.random.randint(0,9,size=(2,3))\n",
    "d3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "1e8fd6da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.5"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(d3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "57001ae9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4. , 2. , 4.5])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(d3,axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "6dc2d304",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3.66666667, 3.33333333])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(d3,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "990fe1c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3.66666667, 3.33333333])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d3.mean(axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa0a7828",
   "metadata": {},
   "source": [
    "4. 求数据的标准差np.std()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "6a28d036",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d4=np.arange(12).reshape(3,4)\n",
    "d4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "00754aa2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.11803399, 1.11803399, 1.11803399])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.std(d4,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "daaec95c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.11803399, 1.11803399, 1.11803399])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d4.std(axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "907844b1",
   "metadata": {},
   "source": [
    "5. 求数据的方差np.var()\n",
    "6. 求数据的最大值np.max()、最小值np.min()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b19784ad",
   "metadata": {},
   "source": [
    "7. 求数据的最小值索引np.argmin()、最大值的索引np.argmax()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "9e719d58",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d7=np.arange(12).reshape(3,4)\n",
    "d7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "1053969b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#当不输入axis参数时，当一维数组进行计算\n",
    "np.argmin(d7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "3b13cd80",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argmin(d7,axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "8f1f080f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argmin(d7,axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62b50118",
   "metadata": {},
   "source": [
    "8. 求数据的中位数np.median"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "9a5629ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 3, 6],\n",
       "       [2, 3, 6]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d8=np.random.randint(0,9,size=(2,3))\n",
    "d8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "63e5d2bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.5"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.median(d8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "55dd05aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4., 3., 6.])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.median(d8,axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "de6303e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6., 3.])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.median(d8,axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee4f94e8",
   "metadata": {},
   "source": [
    "9. python内置的数学函数，同numpy的数学函数实现功能类似，但numpy的执行效率高，速度快。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "c41b0a04",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 94.7 ms, sys: 1.32 ms, total: 96 ms\n",
      "Wall time: 94.4 ms\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "499999500000"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#内置的sum函数执行时间\n",
    "%time sum(np.arange(1000000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "e72e2781",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 4.01 ms, sys: 0 ns, total: 4.01 ms\n",
      "Wall time: 3.45 ms\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "499999500000"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#np.sum()函数的计算时间\n",
    "%time np.sum(np.arange(1000000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "19d00764",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "499999500000\n",
      "CPU times: user 129 ms, sys: 0 ns, total: 129 ms\n",
      "Wall time: 127 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "#计算1-1000000中间各个数字的和\n",
    "s=0\n",
    "for n in range(1000000):\n",
    "    s+=n\n",
    "print(s)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
